﻿unit CmMemberModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TCmMemberModel = class(TObject)
  public 
    const cModuleKey='CmMember'; 
    const cTblName ='cm_member';
    const cBaseView = 'view_cm_member';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cMemberNoKey='member_no';
    const cMemberNameKey='member_name';
    const cMemberCertificateNoKey='member_certificate_no';
    const cMemberPhoneKey='member_phone';
    const cMemberPasswordKey='member_password';
    const cMemberStatusKey='member_status';
    const cMemberRemarkKey='member_remark';
    const cEnterpriseIdKey='enterprise_id';
    const cRecordPsnKey='record_psn';
    const cRecordDateKey='record_date';
    const cModifyPsnKey='modify_psn';
    const cModifyDateKey='modify_date';
    const cTimeStampKey='time_stamp';
  end;
  
  TCmMember = class(TObject)
  private    
    fMemberNo:string;
    fMemberName:string;
    fMemberCertificateNo:string;
    fMemberPhone:string;
    fMemberPassword:string;
    fMemberStatus:Integer;
    fMemberRemark:string;
    fEnterpriseId:string;
    fRecordPsn:string;
    fRecordDate:TDateTime;
    fModifyPsn:string;
    fModifyDate:TDateTime;
    fTimeStamp:Int64;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property MemberNo:string read fMemberNo write fMemberNo;
    property MemberName:string read fMemberName write fMemberName;
    property MemberCertificateNo:string read fMemberCertificateNo write fMemberCertificateNo;
    property MemberPhone:string read fMemberPhone write fMemberPhone;
    property MemberPassword:string read fMemberPassword write fMemberPassword;
    property MemberStatus:Integer read fMemberStatus write fMemberStatus;
    property MemberRemark:string read fMemberRemark write fMemberRemark;
    property EnterpriseId:string read fEnterpriseId write fEnterpriseId;
    property RecordPsn:string read fRecordPsn write fRecordPsn;
    property RecordDate:TDateTime read fRecordDate write fRecordDate;
    property ModifyPsn:string read fModifyPsn write fModifyPsn;
    property ModifyDate:TDateTime read fModifyDate write fModifyDate;
    property TimeStamp:Int64 read fTimeStamp write fTimeStamp;
  end;

  TCmMemberList = class(TDictionary<string,TCmMember>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TCmMember;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TCmMember):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TCmMember;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TCmMemberModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmMemberModel.cTblName;
    dblmTable: ReadName:=TCmMemberModel.cTblName;
    dblmView: ReadName:=TCmMemberModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TCmMemberModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmMemberModel.cTblName;
    dblmTable: ReadName:=TCmMemberModel.cTblName;
    dblmView: ReadName:=TCmMemberModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TCmMemberModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmMemberModel.cTblName;
    dblmTable: ReadName:=TCmMemberModel.cTblName;
    dblmView: ReadName:=TCmMemberModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TCmMember.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TCmMember.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TCmMember.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fMemberNo:=EmptyStr;
  fMemberName:=EmptyStr;
  fMemberCertificateNo:=EmptyStr;
  fMemberPhone:=EmptyStr;
  fMemberPassword:=EmptyStr;
  fMemberStatus:=-1;
  fMemberRemark:=EmptyStr;
  fEnterpriseId:=EmptyStr;
  fRecordPsn:=EmptyStr;
  fRecordDate:=fInitDate;
  fModifyPsn:=EmptyStr;
  fModifyDate:=fInitDate;
  fTimeStamp:=-1;
end;

function TCmMember.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TCmMemberModel.cMemberNoKey)<>nil) then
    begin
      fMemberNo:=iCds.FieldByName(TCmMemberModel.cMemberNoKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fMemberNo);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberNameKey)<>nil) then
    begin
      fMemberName:=iCds.FieldByName(TCmMemberModel.cMemberNameKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberCertificateNoKey)<>nil) then
    begin
      fMemberCertificateNo:=iCds.FieldByName(TCmMemberModel.cMemberCertificateNoKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberCertificateNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberPhoneKey)<>nil) then
    begin
      fMemberPhone:=iCds.FieldByName(TCmMemberModel.cMemberPhoneKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberPhoneKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberPasswordKey)<>nil) then
    begin
      fMemberPassword:=iCds.FieldByName(TCmMemberModel.cMemberPasswordKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberPasswordKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberStatusKey)<>nil) then
    begin
      fMemberStatus:=iCds.FieldByName(TCmMemberModel.cMemberStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberRemarkKey)<>nil) then
    begin
      fMemberRemark:=iCds.FieldByName(TCmMemberModel.cMemberRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cEnterpriseIdKey)<>nil) then
    begin
      fEnterpriseId:=iCds.FieldByName(TCmMemberModel.cEnterpriseIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cEnterpriseIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cRecordPsnKey)<>nil) then
    begin
      fRecordPsn:=iCds.FieldByName(TCmMemberModel.cRecordPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cRecordDateKey)<>nil) then
    begin
      if iCds.FindField(TCmMemberModel.cRecordDateKey).IsNull then
      begin
        fRecordDate:=fInitDate;
      end else begin
        fRecordDate:=iCds.FieldByName(TCmMemberModel.cRecordDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cModifyPsnKey)<>nil) then
    begin
      fModifyPsn:=iCds.FieldByName(TCmMemberModel.cModifyPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cModifyDateKey)<>nil) then
    begin
      if iCds.FindField(TCmMemberModel.cModifyDateKey).IsNull then
      begin
        fModifyDate:=fInitDate;
      end else begin
        fModifyDate:=iCds.FieldByName(TCmMemberModel.cModifyDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cTimeStampKey)<>nil) then
    begin
      fTimeStamp:=iCds.FieldByName(TCmMemberModel.cTimeStampKey).AsLargeInt;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmMember.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TCmMemberModel.cMemberNoKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberNoKey).AsString:=fMemberNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberNameKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberNameKey).AsString:=fMemberName;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberCertificateNoKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberCertificateNoKey).AsString:=fMemberCertificateNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberCertificateNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberPhoneKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberPhoneKey).AsString:=fMemberPhone;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberPhoneKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberPasswordKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberPasswordKey).AsString:=fMemberPassword;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberPasswordKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberStatusKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberStatusKey).AsInteger:=fMemberStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cMemberRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cMemberRemarkKey).AsString:=fMemberRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cMemberRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cEnterpriseIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cEnterpriseIdKey).AsString:=fEnterpriseId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cEnterpriseIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cRecordPsnKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cRecordPsnKey).AsString:=fRecordPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cRecordDateKey)<>nil) then
    begin
      if SameDateTime(fRecordDate,fInitDate) then
      begin
        iCds.FieldByName(TCmMemberModel.cRecordDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TCmMemberModel.cRecordDateKey).AsDateTime:=fRecordDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cModifyPsnKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cModifyPsnKey).AsString:=fModifyPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cModifyDateKey)<>nil) then
    begin
      if SameDateTime(fModifyDate,fInitDate) then
      begin
        iCds.FieldByName(TCmMemberModel.cModifyDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TCmMemberModel.cModifyDateKey).AsDateTime:=fModifyDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmMemberModel.cTimeStampKey)<>nil) then
    begin
      iCds.FieldByName(TCmMemberModel.cTimeStampKey).AsLargeInt:=fTimeStamp;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmMemberModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmMember.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmMember.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TCmMemberModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TCmMemberModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TCmMemberModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TCmMemberModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TCmMemberModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TCmMemberModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TCmMemberModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TCmMemberModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TCmMemberModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('MemberNo')<>nil then
    begin
      fMemberNo:=Json.ItemByName('MemberNo').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MemberName')<>nil then
    begin
      fMemberName:=Json.ItemByName('MemberName').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberName']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MemberCertificateNo')<>nil then
    begin
      fMemberCertificateNo:=Json.ItemByName('MemberCertificateNo').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberCertificateNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MemberPhone')<>nil then
    begin
      fMemberPhone:=Json.ItemByName('MemberPhone').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberPhone']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MemberPassword')<>nil then
    begin
      fMemberPassword:=Json.ItemByName('MemberPassword').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberPassword']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MemberStatus')<>nil then
    begin
      fMemberStatus:=Json.ItemByName('MemberStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MemberRemark')<>nil then
    begin
      fMemberRemark:=Json.ItemByName('MemberRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MemberRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('EnterpriseId')<>nil then
    begin
      fEnterpriseId:=Json.ItemByName('EnterpriseId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'EnterpriseId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordPsn')<>nil then
    begin
      fRecordPsn:=Json.ItemByName('RecordPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordDate')<>nil then
    begin
      fRecordDate:=Json.ItemByName('RecordDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyPsn')<>nil then
    begin
      fModifyPsn:=Json.ItemByName('ModifyPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyDate')<>nil then
    begin
      fModifyDate:=Json.ItemByName('ModifyDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('TimeStamp')<>nil then
    begin
      fTimeStamp:=Json.ItemByName('TimeStamp').AsInt64;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'TimeStamp']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TCmMember.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TCmMemberModel.cModuleKey;
    Json.Add(TCmMemberModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TCmMemberModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TCmMemberModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('MemberNo').AsString:=fMemberNo;
    Json.Add('MemberName').AsString:=fMemberName;
    Json.Add('MemberCertificateNo').AsString:=fMemberCertificateNo;
    Json.Add('MemberPhone').AsString:=fMemberPhone;
    Json.Add('MemberPassword').AsString:=fMemberPassword;
    Json.Add('MemberStatus').AsInteger:=fMemberStatus;
    Json.Add('MemberRemark').AsString:=fMemberRemark;
    Json.Add('EnterpriseId').AsString:=fEnterpriseId;
    Json.Add('RecordPsn').AsString:=fRecordPsn;
    Json.Add('RecordDate').AsDateTime:=fRecordDate;
    Json.Add('ModifyPsn').AsString:=fModifyPsn;
    Json.Add('ModifyDate').AsDateTime:=fModifyDate;
    Json.Add('TimeStamp').AsInt64:=fTimeStamp;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TCmMemberList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TCmMemberList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TCmMemberList.ResetObject;
begin
  Clear;
end;

function TCmMemberList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TCmMember;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TCmMember.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmMemberList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmMemberList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TCmMember;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TCmMemberModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TCmMemberModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TCmMemberModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TCmMemberModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TCmMemberModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TCmMemberModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TCmMemberModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TCmMemberModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TCmMember.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TCmMemberList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TCmMemberModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TCmMemberModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TCmMemberModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TCmMemberList.FindByKey(const iKey:string):TCmMember;
var
  Item:TCmMember;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TCmMemberList.IsExist(iKey:string):Boolean;
var
  Item:TCmMember;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TCmMemberList.Add(const iItem:TCmMember):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TCmMemberList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TCmMemberList.Remove(const iKey:string):TCmMember;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TCmMemberList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
